import VueRouter from 'vue-router'
import { EMBED_NAME, EMBED_ROUTER, IFRAME_ROUTER, IFRAME_NAME } from './utils';
import { AllowedRouters } from './store';

const routers = [{
        path: EMBED_ROUTER,
        name: EMBED_NAME,
        component: () => import('modules/common/embed.vue')
    },
    {
        path: IFRAME_ROUTER,
        name: IFRAME_NAME,
        component: () => import('modules/common/iframe.vue')
    },
    {
        path: "/home",
        component: () => import('modules/home.vue')
    },
    {
        path: "/sys/profile",
        component: () => import('modules/common/profile.vue')
    },
    {
        path: "/sys/more",
        component: () => import('modules/common/more.vue')
    },
    {
        path: "/sys/account/manage",
        component: () => import('modules/security/account/manage.vue'),
        children: [{
                path: "/sys/account/add",
                component: () => import('modules/security/account/add.vue')
            },
            {
                path: "/sys/account/edit",
                component: () => import('modules/security/account/edit.vue')
            },
            {
                path: "/sys/account/role",
                component: () => import('modules/security/account/role.vue')
            }
        ]
    },
    {
        path: "/sys/role/manage",
        component: () => import('modules/security/role/manage.vue'),
        children: [{
                path: "/sys/role/add",
                component: () => import('modules/security/role/add.vue')
            },
            {
                path: "/sys/role/edit",
                component: () => import('modules/security/role/edit.vue')
            },
            {
                path: "/sys/role/perm",
                component: () => import('modules/security/role/perm.vue')
            }
        ]
    },
    {
        path: "/sys/perm/manage",
        component: () => import('modules/security/permission/manage.vue'),
        children: [{
                path: "/sys/perm/add",
                component: () => import('modules/security/permission/add.vue')
            },
            {
                path: "/sys/perm/edit",
                component: () => import('modules/security/permission/edit.vue')
            }
        ]
    },
    {
        path: "/sys/router/manage",
        component: () => import('modules/security/router/manage.vue'),
        children: [{
                path: "/sys/router/add",
                component: () => import('modules/security/router/add.vue')
            },
            {
                path: "/sys/router/edit",
                component: () => import('modules/security/router/edit.vue')
            },
            {
                path: "/sys/router/perm",
                component: () => import('modules/security/router/perm.vue')
            }
        ]
    },
    {
        path: "/sys/menu/manage",
        component: () => import('modules/security/menu/manage.vue'),
        children: [{
                path: "/sys/menu/add",
                component: () => import('modules/security/menu/add.vue')
            },
            {
                path: "/sys/menu/edit",
                component: () => import('modules/security/menu/edit.vue')
            }
        ]
    },
    {
        path: "/sys/dict/manage",
        component: () => import('modules/security/dict/manage.vue'),
        children: [{
                path: "/sys/dict/add",
                component: () => import('modules/security/dict/add.vue')
            },
            {
                path: "/sys/dict/edit",
                component: () => import('modules/security/dict/edit.vue')
            },
            {
                path: "/sys/dict/value/manage",
                component: () => import('modules/security/dict/value/manage.vue'),
                children: [{
                        path: "/sys/dict/value/add",
                        component: () => import('modules/security/dict/value/add.vue')
                    },
                    {
                        path: "/sys/dict/value/edit",
                        component: () => import('modules/security/dict/value/edit.vue')
                    }
                ]
            }
        ]
    },
    {
        path: "/sys/launcher/manage",
        component: () => import('modules/security/launcher/manage.vue'),
        children: [{
                path: "/sys/launcher/add",
                component: () => import('modules/security/launcher/add.vue')
            },
            {
                path: "/sys/launcher/edit",
                component: () => import('modules/security/launcher/edit.vue')
            }
        ]
    },
    {
        path: "/sys/filemanager/manage",
        component: () => import('modules/filemanager/manage.vue'),
        children: [{
                path: "/sys/filemanager/add",
                component: () => import('modules/filemanager/add.vue')
            },
            {
                path: "/sys/filemanager/edit",
                component: () => import('modules/filemanager/edit.vue')
            }
        ]
    },
    {
        path: "/security/home/manage",
        component: () => import('modules/security/home/manage.vue'),
    }
];

const routes = [
    {
        path: '*',
        component: () => import('modules/dev.vue')
    },
    {
        path: '/loading',
        component: () => import('modules/loading.vue')
    },
    {
        path: '/login',
        component: () => import('modules/login.vue')
    },
    {
        path: '/theme/normal',
        component: () => import('modules/theme/normal.vue'),
        children: routers
    },
    {
        path: '/theme/window',
        component: () => import('modules/theme/window.vue'),
        children: routers
    }
];

export const DEFAULT_ROUTERS = [
    "/",
    "/home",
    "/login",
    "/loading",
    "/sys/more",
    "/sys/profile",
    "/theme/normal",
    "/theme/window"
];

export const addAllowedRouter = (router) => {
    if(DEFAULT_ROUTERS.indexOf(router) == -1) {
        DEFAULT_ROUTERS.push(router);
    }
}

export const isRouterPass = (router) => {
    const path = router.substring(1);
    return DEFAULT_ROUTERS.indexOf(router) > -1 
        || AllowedRouters.indexOf(router) > -1 
        || AllowedRouters.indexOf(path) > -1;
}

//重复路由异常处理
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) {
        return originalPush.call(this, location, onResolve, onReject);
    }
    return originalPush.call(this, location).catch(err => err);
}

export const router = new VueRouter({
    mode: 'hash',
    routes // (缩写) 相当于 routes: routes
});

router.beforeEach((to, from, next) => {
    if(to.name == EMBED_NAME || to.name == IFRAME_NAME) {
        if(isRouterPass(to.params.link)) {
            next();
        } else {
            console.log("没有权限访问：" + to.params.link);
            next(false);
        }
        return;
    }
    if(isRouterPass(to.path)) {
        next();    
    } else {
        console.log("没有权限访问：" + to.fullPath);
        next(false);
    }
});

export default VueRouter;